--[========================================================================[--

Menu screen code for Thrust II Reloaded.

Copyright © 2015 Pedro Gimeno Fortea

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

--]========================================================================]--

local menu = {}

local menuimage
local has_save
local t
local playing
local period = 1.9961678004535148 -- 88031 samples / 44100 Hz
local bar1, bar2
local barc

function menu.load()
  menuimage = lg.newImage("img/mainmenu.png")
  snd = la.newSource("snd/menu.wav")
  snd:setLooping(true)
end

function menu.activate()
  has_save = lfs.isFile("saved.txt")
  playing = false
end

function menu.deactivate()
  snd:stop()
end

function menu.pause(paused)
  if playing then
    if paused then snd:pause() else snd:play() end
  end
end

function menu.update(dt)
  if not playing then
    playing = true
    t = period/2
    snd:play()
    bar1 = main.wh/2
    bar2 = bar1
    barc = 1
  else
    t = t + dt
    if t >= period then
      t = t - period
    end
    bar1 = main.wh/2 - (t-period/2)/period * main.wh*0.78
    bar2 = main.wh/2 + (t-period/2)/period * main.wh*0.78
    barc = barc + dt * 50
  end
  lti.sleep(0.02)
end

function menu.draw()
  if playing then
    local c = math.floor(barc) % 6 + 1
    local b = c % 2  c = (c - b) / 2
    local r = c % 2  c = (c - r) / 2
    local g = c % 2
    lg.setColor(r*192, g*192, b*192)
    lg.rectangle("fill", 0, bar1-2, main.ww, 5)
    lg.rectangle("fill", 0, bar2-2, main.ww, 5)
  end
  lg.setColor(255,255,255)
  lg.draw(menuimage, 0, 0)
  lg.setColor(0,192,192)
  lg.print("FROM FIREBIRD SOFTWARE", 144, 144)
  lg.setColor(0,192,0)
  lg.print("@^ A.ROGERS 1986", 192, 176)
  lg.print("@^ P.GIMENO 2015", 192, 200)
  lg.setColor(192,192,192)
  lg.print("1....START  GAME", 192, 272)
  lg.print("2....DEFINE KEYS", 192, 304)
  if has_save then
    lg.print("3....LOAD   GAME", 192, 336)
  end
  lg.setColor(255,255,255)
end

function menu.textinput(c)
  if c == "1" then
    main.restore = false
    game.newgame()
    main.activate(game)
  elseif c == "2" then
    main.activate(redef)
  elseif c == "3" and has_save then
    main.restore = true
    game.newgame()
    main.activate(game)
  end
end

return menu
